package com.edu.admin.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysMenu;
import com.edu.admin.enums.RoleLevelEnum;
import com.edu.admin.mapper.MenuMapper;
import com.edu.admin.vo.MenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 菜单服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class MenuService extends BaseService<MenuMapper, SysMenu> {
	/**
	 * 获取菜单列表
	 * 
	 * @param 
	 * 
	 * @return List<MenuVo>
	 */
	public List<MenuVo> getMenuList() {
		//return this.mapper.getMenuList();
		List<MenuVo> menuList = this.mapper.getMenuListByParentId(0L);
		getSonMenu(menuList,1,0);
		return menuList;
	}
	

	/**
	 * 递归查询子级
	 * 
	 * @param menuList
	 * @param productLevel
	 * @param roleLevel
	 * 
	 * @return
	 * 
	 */
	private void getSonMenu(List<MenuVo> menuList,Integer productLevel,Integer roleLevel) {
		Iterator<MenuVo> iter = menuList.iterator();
		while (iter.hasNext()) {
			MenuVo menu = iter.next();
			List<MenuVo> sonMenuList = this.mapper.getMenuListByLevelAndParent(productLevel,roleLevel,menu.getMenuId());
			if (sonMenuList == null) {
				continue;
			}
			menu.setSonMenuList(sonMenuList);
			getSonMenu(sonMenuList,productLevel,roleLevel);
		}
	}
	
    /**
     * 递归查询父菜单列表
     * 
     * @param menuList
     * @param menuIds
     * 
     * @return List<Long>
     * 
     */
    public List<Long> getMenuParentList(List<MenuVo> menuList, List<Long> menuIds) throws Exception {
        for(MenuVo menu : menuList){
            if(menu.getSonMenuList()!=null && menu.getSonMenuList().size() > 0){
                menuIds.add(menu.getMenuId());//所有父级菜单id集合
                getMenuParentList(menu.getSonMenuList(),menuIds);
            }
        }
        return menuIds;
    }
	
    
    /**
	 * 获取所有菜单列表（分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * 
	 * @return PageInfo<MenuVo>
	 */
	public PageInfo<MenuVo> getMenuListPage(Integer pageNum, Integer pageSize) {
		
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<MenuVo> menuList = getMenuList();
		
		PageInfo<MenuVo> pageInfo = new PageInfo<MenuVo>(menuList);
		
		return pageInfo;
	}
	
	/**
	 * 根据产品id获取菜单列表
	 * 
	 * @param productId
	 * 
	 * @return List<MenuVo>
	 */
	public List<MenuVo> getMenuListByProductId(Long productId) {
		return this.mapper.getMenuListByProductId(productId);
	}
	
	
	/**
	 * 根据层级获取菜单列表
	 * 
	 * @param Integer productLevel
	 * @param Long menuParentId
	 * @return List<MenuVo>
	 */
	public List<MenuVo> getMenuListByRoleLevel(Integer productLevel,Integer roleLevel) {
		List<MenuVo> menuList = this.mapper.getMenuListByLevelAndParent(productLevel,RoleLevelEnum.SUPER.getCode(),0L);
		getSonMenu(menuList,productLevel,roleLevel);
		return menuList;
	}
	
	/**
	 * 根据层级和学校id获取菜单列表
	 * 
	 * @param Integer productLevel
	 * @param Long schoolId
	 * @return List<MenuVo>
	 */
	public List<MenuVo> getMenuListBySchoolId(Integer productLevel,Long schoolId) {
		List<MenuVo> menuList = this.mapper.getMenuListByLevelAndSchoolId(productLevel,RoleLevelEnum.SUPER.getCode(),0L,schoolId);
		getSonMenu(menuList,productLevel,RoleLevelEnum.SCHOOL.getCode());
		return menuList;
	}
	
	
	/**
	 * 菜单保存
	 * 
	 * @param SysMenu
	 * @param SysUser
	 * 
	 * @return
	 */
	public Map<String, Object>  saveMenu(SysMenu menu) {
		Map<String, Object> result = new HashMap<>();
		
		Long menuId = menu.getMenuId();

		if (menuId == null) {
			
			insert(menu);
			
			menuId = menu.getMenuId();


		} else {

			updateByPrimaryKeySelective(menu);

		}
		
		result.put("menuId", menuId);

		return result;

	}


}
